package cn.com.sd.system.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;

import cn.com.sd.system.model.Menu;
import cn.com.sd.system.model.Operator;
import cn.com.sd.system.model.Privilege;
import cn.com.sd.system.model.Role;
import cn.com.sd.system.service.IOperatorService;


public class LoginAction {

	@Resource(name = "operatorService")
	private IOperatorService operatorService;
	
	public String login(){
		return "show";
	}
	
	public String logins(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String  userName = (String) request.getParameter("userName");
		String pwd = (String) request.getParameter("password");
		
		Operator operator = new Operator();
		operator.setName(userName);
		operator.setPassword(pwd);
		Operator result = operatorService.validateLogin(operator);
		
		if (result == null) {
			request.setAttribute("errorMsg", "用户名信息或密码有误");
			return "show";
		}
		
		//获取权限信息
		Set<Privilege> privileges = getPrivilege(result);
		if (CollectionUtils.isEmpty(privileges)) {
			request.setAttribute("errorMsg", "用户没有相应权限信息");
			return "show";
		}
		//获取用户可以访问的所有url信息
		List<String> urls = getUrls(privileges);
		request.getSession().setAttribute(WebCommon.USER_AUTHORITY, urls);
		
		//获取所有左侧的菜单信息
		List<Menu> menus = getMenus(privileges);
		request.getSession().setAttribute(WebCommon.LEFT_MENUS, menus);
		request.getSession().setAttribute(WebCommon.CURRENT_USER_NAME, userName);
		request.getSession().setAttribute(WebCommon.CURRENT_USER, result);
		
		return "main";
	}
	
	private Set<Privilege> getPrivilege(Operator operator) {
		
		Set<Privilege> result = new HashSet<Privilege>();
		Set<Role> roles = operator.getRoles();
		if (!CollectionUtils.isEmpty(roles)) {
			for (Role role : roles) {
				if (role == null || CollectionUtils.isEmpty(role.getPrivileges())) {
					continue;
				}
				result.addAll(role.getPrivileges());
			}
		}
		if (!CollectionUtils.isEmpty(operator.getPrivileges())) {
			result.addAll(operator.getPrivileges());
		}
		return result;
	}
	
	/**
	 * 获取所有的菜单
	 * @param privileges
	 * @return
	 */
	private List<Menu> getMenus(Set<Privilege> privileges) {
		if (CollectionUtils.isEmpty(privileges)) {
			return null;
		}
		List<Menu> result = new ArrayList<Menu>();
		for (Privilege privilege : privileges) {
			if (privilege == null) {
				continue;
			}
			Menu menu = privilege.getMenu();
			if (menu != null) {
				result.add(menu);
			}
		}
		return parseLeftMenus(result);
	}
	
	/**
	 * 过滤出所有左侧的菜单
	 * @param menus
	 * @return
	 */
	private List<Menu> parseLeftMenus(List<Menu> menus) {
		Map<Menu, List<Menu>> map = new HashMap<Menu, List<Menu>>();
		Collections.sort(menus, new Comparator<Menu>() {
			@Override
			public int compare(Menu o1, Menu o2) {
				return o1.getIndex().compareTo(o2.getIndex());
			}
		});
		for (Menu menu : menus) {
			if (menu == null) {
				continue;
			}
			if (menu.getTarget().equals("0")) {
				List<Menu> temp = new ArrayList<Menu>();
				map.put(menu, temp);
			}
		}
		for (Menu menu : menus) {
			if (menu == null) {
				continue;
			}
			if (menu.getTarget().equals("1")) {
				System.out.println(menu.getParentMenu()+"--");
				map.get(menu.getParentMenu()).add(menu);
			}
		}
		
		Set<Menu> menuKeys = map.keySet();
		List<Menu> menuKeyList = new ArrayList<Menu>();
		menuKeyList.addAll(menuKeys);
		Collections.sort(menuKeyList, new Comparator<Menu>() {
			@Override
			public int compare(Menu o1, Menu o2) {
				return o1.getIndex().compareTo(o2.getIndex());
			}
		});
		
		List<Menu> result = new ArrayList<Menu>();
		for (Menu menuOne : menuKeyList) {
			result.add(menuOne);
			result.addAll(map.get(menuOne));
		}
		for (Menu n : result) {
			System.out.println(n.getIndex() + "--" + n.getName());
		}
		return result;
	}
	
	/**
	 * 获取所有的url
	 * @param privileges
	 * @return
	 */
	private List<String> getUrls(Set<Privilege> privileges) {
		if (CollectionUtils.isEmpty(privileges)) {
			return null;
		}
		List<String> result = new ArrayList<String>();
		for (Privilege privilege : privileges) {
			if (privilege == null) {
				continue;
			}
			Menu menu = privilege.getMenu();
			if (menu == null || StringUtils.isEmpty(menu.getUrl())) {
				continue;
			}
			result.add(menu.getUrl());
		}
		return result;
	}
}

